package com.xbongbong.paas.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.UniqueIndexPojo;
import com.xbongbong.paas.pojo.dto.ExcelExplainGetDTO;
import com.xbongbong.paas.pojo.dto.ExcelTemplateDTO;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.ImportOtherParamsDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.pojo.vo.ExportFormDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.webimport.LinkFormImportPojo;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;
import java.util.Map;

/**
 * 导入时一些逻辑处理Service层
 *
 * @author wufeng
 * @date 2018/9/21 14:45
 */
public interface HandleImportService {

    /**
     * PaaS初始化表单的一些基础数据
     *
     * @param importFormDataDTO 导入入参
     * @return PaasFormDataEntity
     * @throws XbbException XbbException
     * @author GuoJun.HU
     * @date 2019/4/29 14:19
     * @since V1.0
     */
    PaasFormDataEntity getPaasForm(ImportFormDataDTO importFormDataDTO) throws XbbException;

    /**
     * 特殊字段解释处理
     *
     * @param fieldAttrList     解释
     * @param excelTemplateDTO 额外参数
     * @return JSONArray
     * @throws XbbException XbbException
     * @author GuoJun.HU
     * @date 2019/4/24 15:51
     * @since V1.0
     */
    JSONArray handleSpecialExplain(List<FieldAttrEntityForImport> fieldAttrList, ExcelTemplateDTO excelTemplateDTO) throws XbbException;

    /**
     * 获取Excel表头
     *
     * @param workBook      excel
     * @param titleRowIndex 标题所在的行
     * @return List
     * @throws XbbException XbbException
     * @author GuoJun.HU
     * @date 2019/4/25 19:31
     * @since V1.0
     */
    List<CellTitlePojo> getExcelTitle(Workbook workBook, int titleRowIndex, Workbook tempExcel) throws XbbException;

    /**
     * 获取Excel数据
     *
     * @param workBook excel文件
     * @param rowIndex 用该行来判断该excel文件有多少列数据
     * @return String[][]
     * @throws XbbException XbbException
     * @author chy
     * @date 很久之前
     */
    String[][] getExcelValue(Workbook workBook, int rowIndex) throws XbbException;


    /**
     * Description: 处理合同和报价单整单折扣的情况，防止下面数字校验出问题
     * @param rowGroupPojoList
     * @return List
     * @throws XbbException
     * @author 刘阳
     * @date 2019/10/9 2:35 PM
     * @since v1.0
     */
    List<RowGroupPojo> handleDiscount(List<RowGroupPojo> rowGroupPojoList, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 获取业务用于唯一标识的字段在Excel中的列索引
     *
     * @param businessType  业务
     * @param fieldAttrList 解释
     * @param titlePojoList 表头
     * @return Integer
     * @author GuoJun.HU
     * @date 2019/4/29 16:40
     * @since V1.0
     */
    UniqueIndexPojo getUniqueIdentityColumnIndex(Integer businessType, List<FieldAttrEntityForImport> fieldAttrList, List<CellTitlePojo> titlePojoList);
    
    /**
     * Description:获取报价单业务用于唯一标识的字段在Excel中的列索引
     * @param fieldAttrList 解释
     * @param titlePojoList 表头
     * @return Integer
     * @author 刘阳
     * @date 2019/10/21 10:19 AM
     * @since v1.0
     */
    List<Integer> getQuotationIdentityColumnIndex(List<FieldAttrEntityForImport> fieldAttrList, List<CellTitlePojo> titlePojoList);

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO   导入入参
     * @param titlePojoList       标题
     * @param rowGroupPojo        一个表单对应的一组Excel填写的数据
     * @param fieldAttrList       解释
     * @param contactLabelAttr
     * @param contactLabelPojoMap
     * @return FormDataAddDTO
     * @throws XbbException XbbException
     * @author GuoJun.HU
     * @date 2019/4/29 14:16
     * @since V1.0
     */
    FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList, String contactLabelAttr, Map<String, JSONObject> contactLabelPojoMap) throws XbbException;

    /**
     * 过滤不需要导入的字段
     *
     * @param fieldAttrList 解释
     * @param saasMark saas标识
     * @param fromImport 来自与导入为1，导出为2
     * @param businessType 业务类型
     * @param corpid 公司id
     * @param formId 表单id
     * @throws XbbException 异常
     */
    void filterUnImportFields(List<FieldAttrEntityForImport> fieldAttrList, Integer saasMark, Integer fromImport, Integer businessType, String corpid, Long formId,Integer isFileImport) throws XbbException;

    /**
     * 过滤不需要导入的字段
     *
     * @param fieldAttrList 解释
     * @param saasMark saas标识
     * @param fromImport 来自与导入为1，导出为2
     * @throws XbbException 异常
     */
    void filterUnExportFields(String corpid, List<FieldAttrEntityForImport> fieldAttrList, Integer saasMark, Integer fromImport, Integer businessType, Boolean partialExportFlag) throws XbbException;

    /**
     * 待系列字段处理
     *
     * @param fieldAttrList 解释
     * @throws XbbException 异常
     */
    void waitSeriesFields(List<FieldAttrEntityForImport> fieldAttrList, FormDataListDTO formDataListDTO ) throws XbbException;


    /**
     * 处理公有的需要转换的字段
     *
     * @param fieldAttrList 解释
     * @throws XbbException 异常
     */
    void handleCommonNeedTransferFields(List<FieldAttrEntityForImport> fieldAttrList,FormDataListDTO formDataListDTO) throws XbbException;

    /**
     * 处理关联新建表单，如客户新建联系人，合同新建回款
     *
     * @param linkFormImportPojo 入参
     * @throws XbbException 异常
     */
    void handleLinkForm(LinkFormImportPojo linkFormImportPojo, Integer distributorMark) throws XbbException;

    /**
     * saas导入保存
     * @param importFormDataDTO 导入前端参数
     * @param totalRowNum 总行数
     * @param rowGroupPojoList 各行数据
     * @param titlePojoList 标题
     * @param fieldAttrEntityForImportList 需要批量查询的数据
     * @param checkRepeatKey
     * @throws XbbException
     */
    void  importSaasData(ImportOtherParamsDTO importOtherParamsDTO, ImportFormDataDTO importFormDataDTO, int totalRowNum, List<RowGroupPojo> rowGroupPojoList, List<CellTitlePojo> titlePojoList, List<FieldAttrEntityForImport> fieldAttrEntityForImportList, String checkRepeatKey) throws Exception;
    /**
     * 导出时格式化数据
     *
     * @param paasFormDataESList 数据
     * @param formDataListDTO 入参
     * @throws XbbException 异常
     */
    void formatExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * Description: 导出时格式化数据
     * @param formDataListDTO
     * @param exportFormDataVO
     * @param suffix
     * @return java.lang.String
     * @throws XbbException
     * @author 刘阳
     * @date 2019/6/9 10:37 AM
     * @since v1.0
     */

    String exportAsync(FormDataListDTO formDataListDTO, ExportFormDataVO exportFormDataVO, String suffix) throws XbbException;

    /**
     * 改变showType
     *
     * @param excelTemplateDTO
     * @param fieldAttrList
     */
    void changeFieldShowTypeForImport(ExcelTemplateDTO excelTemplateDTO, List<FieldAttrEntityForImport> fieldAttrList);

    /**
     * 获取paas的分组数据
     * @param workBook
     * @param startRowIndex
     * @param fieldAttrList
     * @param titlePojoList
     * @return
     */
    List<RowGroupPojo> getPaasExcelValue(Workbook workBook, int startRowIndex, List<FieldAttrEntityForImport> fieldAttrList, List<CellTitlePojo> titlePojoList) throws XbbException;

    /**
     * 标题必填和分行标签
     * @param fieldAttrList
     */
    void setTitleTag(List<FieldAttrEntityForImport> fieldAttrList);

    void handleLabelFields(List<FieldAttrEntityForImport> fieldAttrList, FormDataListDTO formDataListDTO) throws XbbException;
    /**
     * 待出库导出时格式化数据
     *
     * @param excelExplainGetDTO 数据
     * @param fieldAttrList 入参
     * @throws XbbException 异常
     */
    void waitOutstockExplains(ExcelExplainGetDTO excelExplainGetDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    /**
     * 待采购导出时格式化数据
     *
     * @param excelExplainGetDTO 数据
     * @param fieldAttrList 入参
     * @throws XbbException 异常
     */
    void parseWaitPurchaseExplains(ExcelExplainGetDTO excelExplainGetDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;

    void parseSmartReplenishmentExplains(ExcelExplainGetDTO excelExplainGetDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;
}
